#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010

@author: lxd
'''
import wx
import threading
import tsina

class GetTwitterThread(threading.Thread):        
    def __init__(self, window):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.window = window
        
    def stop(self):
        self.timeToQuit.set()
          
    def run(self):
        tsinaWeb = tsina.TsinaWeb()
        f = tsinaWeb.getMain()
        wx.CallAfter(self.window.getTwitterCall, f)

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title=u'迷你博客', style=wx.DEFAULT_FRAME_STYLE)        
        self.palMain = wx.Panel(self)                
        self.lblTop = self.createTop()        
        self.lstMessage = wx.ListBox(self.palMain, -1, size=(500, 600), style=wx.LB_SINGLE)   
        self.layout() 
        GetTwitterThread(self).start()
        self.Center() 


    def createTop(self):
        lblTop = wx.StaticText(self.palMain, -1, u"迷你博客")
        lblTop.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        return lblTop

   
    def layout(self):
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.lblTop, 0, wx.ALL, 5)
        mainSizer.Add(wx.StaticLine(self.palMain), 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)
        mainSizer.Add(self.lstMessage, 1, wx.EXPAND)
        self.palMain.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)
        
    def getTwitterCall(self, f):
        ts = tsina.parseTsina(f)
        if ts:
            for t in ts:
                self.lstMessage.Append(t) 
        else:
            self.lstMessage.Append('no twitter data')

                 
if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()


